using System;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;

namespace CodePlex.DnD4eCampaignManager.DnD4e
{
    public class Initiative : IInitiative, IEquatable<Initiative>
    {
        public Initiative()
        {
            InitiativeRoll = new Random().Next(1, 20);
        }

        public int InitiativeModifier { get; set; }
        public int InitiativeRoll { get; set; }

        public int InitiativeScore
        {
            get { return InitiativeRoll + InitiativeModifier; }
            set { InitiativeRoll = value - InitiativeModifier; }
        }

        public override bool Equals(object obj)
        {
            if ( ReferenceEquals(null, obj) )
            {
                return false;
            }
            if ( ReferenceEquals(this, obj) )
            {
                return true;
            }
            if ( obj.GetType() != typeof ( Initiative ) )
            {
                return false;
            }
            return Equals((Initiative) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ( InitiativeModifier * 397 ) ^ InitiativeRoll;
            }
        }

        public bool Equals(Initiative other)
        {
            if ( ReferenceEquals(null, other) )
            {
                return false;
            }
            if ( ReferenceEquals(this, other) )
            {
                return true;
            }
            return other.InitiativeModifier == InitiativeModifier && other.InitiativeRoll == InitiativeRoll;
        }

        public static bool operator ==(Initiative left, Initiative right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Initiative left, Initiative right)
        {
            return !Equals(left, right);
        }
    }
}